﻿using System;
using System.IO;
using System.Data;
using System.Data.Odbc;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;

namespace Ebay
{
	static class Common
	{
		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			try
			{
				Application.Run(new FormMain());
			}
			catch (Exception x)
			{
				File.AppendAllText(Application.ExecutablePath + ".err", x.GetType() + "\r\n" + x.Message + "\r\n" + x.StackTrace);
			}
		}

		public static OdbcConnection GetConnection()
		{
			Assembly assembly = Assembly.GetExecutingAssembly();

			string t = assembly.FullName.Substring(0, assembly.FullName.IndexOf(',')) + ".mdb";
			DirectoryInfo dir = new FileInfo(assembly.CodeBase.Replace("file:///", string.Empty)).Directory;
			FileInfo[] fls = dir.GetFiles(t);
			while (dir != dir.Root && fls.Length == 0)
			{
				dir = dir.Parent;
				fls = dir.GetFiles(t);
			}

			if (fls.Length == 0)
			{
				MessageBox.Show(string.Format("数据库文件[{0}]不存在！", t));
				return null;
			}

			t = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fls[0].FullName;
			OdbcConnection con = new OdbcConnection(t);
			con.Open();
			return con;
		}

	}
}